using UnityEngine;

public class MushroomController : MonoBehaviour, IFruitController
{
	[Header("State")]
	public int amountOfMushrooms;

	public static GameObject[] mushrooms;

	private float newCreation;

	private const int maxAmountOfMushrooms = 50;

	private const float timeBetweenMushroomCreation = 0.65f;

	private void Update()
	{
		if (amountOfMushrooms < 50 && !(Time.timeSinceLevelLoad < newCreation))
		{
			CreateMushroom();
		}
	}

	private void CreateMushroom()
	{
		GameObject gameObject = Object.Instantiate(mushrooms[(Random.value > 0.7f) ? 1 : 0]);
		Vector2Int point = Map.FindNearlestEmptyPoint(new Vector3(Random.Range(0, Map.width) * 2, Random.Range(0, Map.height) * 2), GridType.Soil);
		gameObject.GetComponent<Fruit>().Begin(this, point);
		newCreation = Time.timeSinceLevelLoad + 0.65f;
		amountOfMushrooms++;
	}

	public void EatFruit()
	{
		amountOfMushrooms--;
	}
}
